package com.zhuanzhuan.hero.user.util;

import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @ClassName ServiceHeaderUtil
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/21 16:35
 */
public class ServiceHeaderUtil {

    private static final String TOKEN_KEY = "tk";

    public static Long parseUid (ServiceHeaderEntity header) {
        if (Objects.isNull(header) || NumConsts.ZERO_L.equals(header.getUid())) {
            throw new BusinessException(ExceptionMsgEnum.LOGIN_ERROR);
        }

        return header.getUid();
    }

    public static String parseTvalue (ServiceHeaderEntity header) {
        if (Objects.isNull(header) || Objects.isNull(header.getT())) {
            throw new BusinessException("t值不可为空");
        }

        return header.getT().trim();
    }

    /**
     * 获取设备版本
     *
     * @param header
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/13
     **/
    public static String parseVsersion(ServiceHeaderEntity header) {
        return StringUtils.isBlank(header.getV()) ? StringUtils.EMPTY : header.getV().trim();
    }

    /**
     * 从cookie中解析数据
     *
     * @param header
     * @param key
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/13
     **/
    private static String getValFromCookieMap(ServiceHeaderEntity header, String key) {
        if (header == null || header.getCookieMap() == null) {
            return "";
        }
        String value = header.getCookieMap().get(key);
        if (value == null) {
            return null;
        }
        return value.trim();
    }
}
